package com.mall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.domain.dto.SearchForm;
import com.mall.domain.po.UserRole;
import com.mall.domain.vo.UserRoleVO;
import com.mall.mapper.UserRoleMapper;
import com.mall.service.RoleService;
import com.mall.service.UserRoleService;
import com.mall.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;



@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;


    @Override
    public Page<UserRoleVO> getUserList(SearchForm searchForm,Integer currentPage, Integer pageSize) {
        // 1. 创建分页对象
        Page<UserRoleVO> page = new Page<>(currentPage, pageSize);

        // 2. 直接调用Mapper，传递分页对象和查询条件（无需QueryWrapper）
        return userRoleMapper.getUserList(page, searchForm);
    }

    @Override
    public void assignRoleToUser(String userId, String roleId) {
        QueryWrapper<UserRole> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        UserRole userRole1 = this.getOne(qw);
        if (userRole1 == null){
            UserRole userRole = new UserRole();
            userRole.setUserId(Long.valueOf(userId));
            userRole.setRoleId(Long.valueOf(roleId));
            this.save(userRole);
            return;
        }
        userRole1.setRoleId(Long.valueOf(roleId));
        this.updateById(userRole1);
    }
}
